package finedev.work.vennue;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FriendListAdapter extends ArrayAdapter<FriendItem> {
	
	private Context mContext = null;
	private int mViewResourceId = 0;
	private ArrayList<FriendItem> mContentList = null;
	private LayoutInflater mInflater;
	
	public FriendListAdapter(Context context, int textViewResourceId, ArrayList<FriendItem> contentList) 
	{
		super(context, textViewResourceId, contentList);
		
		mContext = context; // 생성자 초기화 과정
		mViewResourceId = textViewResourceId;
		mContentList = contentList;
		
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}
	
	public View getView(int position, View convertView, ViewGroup parent)
	{
		FriendItem contentItem = mContentList.get(position);
		
		if ( convertView == null ) {
			convertView = mInflater.inflate(mViewResourceId, null);
		}
		
		if ( contentItem != null ) {
			TextView name = (TextView) convertView.findViewById(R.id.findfriendrow_textview_name);
			ImageView profileImage = (ImageView) convertView.findViewById(R.id.findfriendrow_imageview_profile);
			
			name.setText( contentItem.getName() );
			
			try {
				URL url = new URL( contentItem.getSmallPicture() );
				Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
			    profileImage.setImageBitmap(bitmap);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
		    
//			profileImage.setImageURI( Uri.parse( contentItem.getSmallPicture() ) );
		}
		
		return convertView;
		
	}

}
